<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/11/7
  Time: 21:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/mb/jsp/common.jsp" %>

<%@ page import="com.wsu.search.tools.Tools" %>

<%
    //取得url带过来的参数
    //String orderidlist = Tools.RmNull(request.getParameter("orderidlist"));
    String orderid = Tools.RmNull(request.getParameter("orderid"));
 	/* if(CacheClass.isEmpty(orderid))
    {
 		out.println("<script language='JavaScript'>alert('参数不正确！');window.history.back(-1);</script>"); //后退并刷新
 	    return;
 	} */

%>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的收货地址</title>

    <link rel="stylesheet" href="js/validation/screen.css" />
<script src="js/validation/jquery.js"></script>
<script src="js/validation/jquery.validate.js"></script>
<script src="js/validation/messages_zh.js"></script>
<script src="mb/js/jquery.cityselect.js"></script>
<script src="mb/js/city.min.js"></script>

</head>
<body ontouchstart="">
<div class="detailTop pf w100 of">
    <div class="w100 of tc c3 f18">
        <span class="pa" style="left: 5px;" onclick="javascript:history.back();"><img src="mb/images/top_back.png"
                                                                                      style="margin-top: 4px;height: 40px;width: 40px;"/></span>
        我的收货地址
    </div>
</div>

<form method="post" id="signupForm" action="" class="container jz h100" style="padding-top: 41px;">

    <section class="w88 jz pr" style="margin-top: 35px;">
        <div id="city_3" class="w100 pr clearfix">
            <div class="fl f14" style="width: 50%;line-height: 38px;">
                省份：<select name="prov" class="prov" id="prov" required="required"></select>
            </div>
            <div class="fr f14" style="width: 50%;line-height: 38px;">
                城市：<select name="city" class="city" id="city" disabled="disabled" required="required"></select>
            </div>
        </div>
    </section>
    <section class="w88 jz pr" style="margin-top: 22px;">
        <div class="addressBlock w100">
            <textarea required="required" placeholder="详细地址" id="detailAddress"></textarea>
        </div>
    </section>
    <section class="w88 jz pr" style="margin-top: 22px;">
        <div class="addressBlock w100">
            <input type="text" name="username" value="" placeholder="联系人" required="required" id="username"
                   class="addressInput"/>
        </div>
    </section>
    <section class="w88 jz pr" style="margin-top: 22px;">
        <div class="addressBlock w100">
            <input type="tel" name="usertel" value="" placeholder="联系电话" required="required" id="usertel"
                   class="addressInput"/>
        </div>
    </section>
    <div class="detailBottm pf w100 of">
        <button type="button" class="db w100 f18 cf f18 tc" id="addressSub" onclick="sendGoods(m,orderid,address);">确定</button>
    </div>
</form><!--container-->

<script>

// 联系电话(手机/电话皆可)验证 
jQuery.validator.addMethod("isPhone", function(value,element) { 
  var length = value.length; 
  var mobileTel = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/; 
  var tel = /^\d{3,4}-?\d{7,9}$/; 
  return this.optional(element) || mobileTel.test(value); 

}, "请正确填写您的联系电话"); 


      var myValidator;
    $().ready(function(){
    	$("#city_3").citySelect({prov: "省份", city: "城市", required: false});
    
    	  myValidator=$("#signupForm").validate(
	 		{
	 			rules:{
  	 				prov: {
                		required:true,
                		isShengcity:true
            		},			
  	 				city: {
                		required:true,
                		isShengcity:true
            		},
            		detailAddress:{
            			required:true,
             			rangelength:[5,50],
             			isDetailAddress:true 
            		},
            		username:
            		{
            			required:true,
            			rangelength:[2,15]
            		},
            		usertel:
            		{
            			required:true,
            			digits:true,
            			isPhone: true 
	 				}
	 			},
	 			//errorElement: 'p',
                errorPlacement: function(error, element) {
                // Append error within linked label
                
                $( element )
	                .closest( "form" )
	                .find( element )
	                .parent("div")
	                .after( error );
                },
                errorElement: "p"
				
	 		});
    })


     
   
     
     //获取url中的参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
        }
        
        
     var orderidKind = "<%=orderid%>";
    
    //判断网页参数是否出错
     if(orderidKind==""||orderidKind== null){
       showToast("网页参数错误 ！");
       showProgressDg();
       setTimeout('window.history.back();', 1600);
     }
     
     
     var orderid = orderidKind.split("_")[0];
     var Kind = orderidKind.split("_")[1];
	  var m = "";
		if(Kind == "shengwang"){
		  m = 1666;
		}
		if(Kind == "huigou"){
		  m = 1676;
		}
		
		var address ="";
    // 发货
	function sendGoods(m,orderid,address) {
			if(myValidator.form()== false){
	            showToast("填写存在错误 ！");
	            return false;
	        }
	        
	       var prov = $.trim($(".prov").find("option:selected").val());
        	var city = $.trim($(".city").find("option:selected").val());
            var username = $.trim($('#username').val());
            var usertel = $.trim($('#usertel').val());
            var detailAddress = $.trim($('#detailAddress').val());
            
            
            
            if (prov == "请选择" || city == "请选择") {
               	showToast("你尚未选择'省份'或者'城市'");
               	return false;
		    }
		    
		    //按要求过滤
            if(detailAddress.indexOf("_")>-1){
            	showToast("您的详细地址中存在干扰字符，例如（下划线）！");
               	return false;
            }
            
             // 组装地址 收货地址_联系人_电话（收货地址＝省+市+详细地址）
           address = prov.split("_")[0] + city.split("_")[0] + detailAddress + "_" + username + "_" + usertel;
           
           
          
	
         $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m:m,orderid:orderid,address:address}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
				showProgressDg();
            },
            success: function (data) {
               if (data.state == 1) {
                  // 成功弹出提示
                  showToast(data.info);
                  setTimeout('window.history.back()', 1600); 
                } else {
                    // 其他错误弹出提示
                   showToast(data.info);
                }
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
    } 
    
    
     $().ready(function () {
        //取上次的收货地址
        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 146}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg();
            },
            success: function (data) {
                console.log(data);
                if (data.state == 1) {
                    // 申请成功 关闭页面 回退
                    if (typeof(data.obj.address) != "undefined") {
                        // 设置默认地址
                        $.getJSON("mb/js/city.min.js", function (json) {
                            $.each(json.citylist, function (i, prov) {
                                if (prov.p.indexOf(data.obj.shengid) != -1) {
                                    $.each(prov.c, function (i, city) {
                                        if (city.n.indexOf(data.obj.cityid) != -1) {
                                            $("#city_3").citySelect({prov: prov.p, city: city.n, required: false});

                                            // 填写详细地址
                                            var sc = prov.p.split('_')[0] + city.n.split('_')[0];
                                            $("#detailAddress").val(data.obj.address.substring(sc.length, data.obj.address.length));
                                        	$("#username").val(data.obj.person_name);
                                        	$("#usertel").val(data.obj.tel);
                                        	
                                        	return true;
                                        }
                                    });
                                    return true;
                                }
                            });
                        });
                    }

                } else {
                    // 其他错误弹出提示
                    showToast(data.info);
                }
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });

    });
    

</script>
</body>
</html>
